﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using devtm.Configuration.Models;
using System.Reflection;
using devtm.Helpers;
using devtm.Editor.Configuration.Templates;
using devtm.Configuration;

namespace devtm.Editor.Configuration
{
    public static class OtherHelper
    {

        public static string GetPath(this XElement element)
        {
            string a = element.Name.LocalName;

            if (element.Parent != null)
                a = GetPath(element.Parent) + "/" + a;

            return a;
        }

        public static string GetPath(this XAttribute attribute)
        {
        
            string a = string.Format("{0}[{1}]", attribute.Parent.GetPath(), attribute.Name);
            return a;
        }


        /// <summary>
        ///  return the Designer attribute
        /// </summary>
        /// <param name="propertyInfo"></param>
        /// <returns></returns>
        public static CustomDesignerAttribute GetDesignAttribute(this PropertyInfo propertyInfo, IElementSection section)
        {
            Attribute[] Atts = Attribute.GetCustomAttributes(propertyInfo);
            foreach (Attribute item in Atts)
                if (item.GetType().IsOfType(typeof(CustomDesignerAttribute)))
                    return (CustomDesignerAttribute)item;

            return DictionaryTemplate.GetCustomDesignerAttribute(propertyInfo, section);

        }

        /// <summary>
        ///  return the Designer attribute
        /// </summary>
        /// <param name="propertyInfo"></param>
        /// <returns></returns>
        public static IEnumerable<PropertyItemAttribute> GetPropertyItemAttribute(this PropertyInfo propertyInfo, IElementSection section)
        {
            Attribute[] Atts = Attribute.GetCustomAttributes(propertyInfo);
            foreach (Attribute item in Atts)
                if (item.GetType().IsOfType(typeof(CustomDesignerAttribute)))
                    yield return (PropertyItemAttribute)item;

            foreach (PropertyItemAttribute item2 in DictionaryTemplate.GetPropertyItemAttribute(propertyInfo, section))
                yield return item2;

        }

    }
}
